$(function () {
    //0 显示批次信息复选框全选事件
    $("#projectResultInformationShowFather").click(function () {
        var isChecked = $(this).prop("checked")
        if (isChecked) {
            $(".projectResultInformationShowSon").prop("checked", true);
        } else {
            $(".projectResultInformationShowSon").prop("checked", false);
        }
    });


    getDataList();

    //动态创建表格
    function getDataList() {
        $.ajax({
            url: "/hospital/result/all.action",
            type: "GET",
            dataType: "json",
            success: function (data) {
                var $list = $("#tb_projectResult");
                $.each(data, function (index, ele) {
                    var $item = createDataItem(index, ele);
                    $list.append($item);
                });
            },
            error: function (jqXHR) {
                alert(jqXHR.state());
            }
        });
    }

    //动态创建一条数据
    function createDataItem(index, data) {
        var $item = $("" + "<tr>\n" +
            "            <td style=\"width:30px;\"><input class=\"projectResultInformationShowSon\" type=\"checkbox\"></td>\n" +
            "            <td>"+data.phyExmBatchName+"</td>\n" +
            "            <td>"+data.stuName+"</td>\n" +
            "            <td>"+data.proName+"</td>\n" +
            "            <td>"+data.phyExmConclusion+"</td>\n" +
            "            <td>\n" +
            "                <button class=\"projectResultInformationShowUpdateBtn\" data-id=\""+data.id+"\">编辑</button>\n" +
            "                <button class=\"projectResultInformationShowCancelBtn\" data-id=\""+data.id+"\">删除</button>\n" +
            "            </td>\n" +
            "        </tr>");
        return $item;
    }

   //添加新的结果信息按钮监听
   $("#projectResultAddBtn").click(function () {
       // alert("添加信息");
       $(".projectResultAddNewResult").css("display","block");
       $(".projectResultInformationShow").css("display","none");
   }) ;

   //确认按钮监听事件
    $("#projectResultAddNewResultSureBtn").click(function () {
        $(".projectResultAddNewResult").css("display","none");
        $(".projectResultInformationShow").css("display","block");
        //数据传输

    });
    //取消按钮监听事件
    $("#projectResultAddNewResultCancelBtn").click(function () {
        $(".projectResultAddNewResult").css("display","none");
        $(".projectResultInformationShow").css("display","block");
    });
    //获取所有批次*******************
    $("#projectResultSelectBatch").html("");
    $("#projectResultSelectBatch").append("<option value=''>请选择体检批次</option>");
    $.ajax({
        url: "/hospital/batch/all.action",
        type: "GET",
        dataType: "json",
        success: function (data) {
            var $batchlist = $("#projectResultSelectBatch");
            $.each(data, function (index, ele) {
                var $item = $("" + "<option value = '" + ele.id + "'>" + ele.PhyExmBatchName + "</option>");
                $batchlist.append($item);
            });
        },
        error: function () {
            alert("获取批次失败");
        }
    });
    //获取所有学院***********
    $("#projectResultSelectDeparment").html("");
    $("#projectResultSelectDeparment").append("<option value=''>请选择学院</option>");
    $.ajax({
        url: "/hospital/department/all.action",
        type: "GET",
        dataType: "json",
        success: function (data) {
            var $batchlist = $("#projectResultSelectDeparment");
            $.each(data, function (index, ele) {
                var $item = $("" + "<option value = '" + ele.id + "'>" + ele.departName + "</option>");
                $batchlist.append($item);
            });
        },
        error: function () {
            alert("获取学院失败");
        }
    });
    //获取所有项目***********
    $("#projectResultSelectProject").html("");
    $("#projectResultSelectProject").append("<option value=''>请选择项目</option>");
    $.ajax({
        url: "/hospital/project/all.action",
        type: "GET",
        dataType: "json",
        success: function (data) {
            var $batchlist = $("#projectResultSelectProject");
            $.each(data, function (index, ele) {
                var $item = $("" + "<option value = '" + ele.id + "'>" + ele.proName+ "</option>");
                $batchlist.append($item);
            });
        },
        error: function () {
            alert("获取项目失败");
        }
    });
   //查询按钮监听***********
    $("#projectResultSelectBatchBtn").click(function () {
        // alert("按照批次查询");
        var result=$("#projectResultSelectBatch option:selected").val();
        // alert(result);
        $.ajax({
           url:"/hospital/result/batch.action",
           type:"POST",
           contentType:"application/json;charset=utf-8",
           dataType:"JSON",
           data:JSON.stringify({"phyExmBatchId":result}),
           success:function (data) {
               var $list = $("#tb_projectResult");
               $("#tb_projectResult tr:not(:first)").html("");
               $.each(data, function (index, ele) {
                   var $item = createDataItem(index, ele);
                   $list.append($item);
               });
           } ,
            error:function () {
                alert("查询失败");
            }
        });
    });
    $("#projectResultSelectDeparmentBtn").click(function () {
        // alert("按照学院查询");
        var result=$("#projectResultSelectDeparment option:selected").val();
        // alert(result);
        $.ajax({
            url:"/hospital/result/department.action",
            type:"POST",
            contentType:"application/json;charset=utf-8",
            dataType:"JSON",
            data:JSON.stringify({"deptId":result}),
            success:function (data) {
                var $list = $("#tb_projectResult");
                $("#tb_projectResult tr:not(:first)").html("");
                $.each(data, function (index, ele) {
                    var $item = createDataItem(index, ele);
                    $list.append($item);
                });
            } ,
            error:function () {
                alert("查询失败");
            }
        });
    });
    $("#projectResultSelectProjectBtn").click(function () {
        // alert("按照项目名称查询");
        var result=$("#projectResultSelectProject option:selected").val();
        $.ajax({
            url:"/hospital/result/project.action",
            type:"POST",
            contentType:"application/json;charset=utf-8",
            dataType:"JSON",
            data:JSON.stringify({"proId":result}),
            success:function (data) {
                var $list = $("#tb_projectResult");
                $("#tb_projectResult tr:not(:first)").html("");
                $.each(data, function (index, ele) {
                    var $item = createDataItem(index, ele);
                    $list.append($item);
                });
            } ,
            error:function () {
                alert("查询失败");
            }
        });
    });
    //编辑按钮监听事件
    $("#tb_projectResult").delegate(".projectResultInformationShowUpdateBtn","click",function () {
        // alert("编辑操作");
        $(".projectResultUpdate").css("display","block");
        $("#over").css("display","block");
        var value=$(this).parents("tr")[0];
        var id=$(this).attr("data-id");//获取id
        var value1=value.children[1].innerHTML;
        var value2=value.children[2].innerHTML;
        var value3=value.children[3].innerHTML;
        var value4=value.children[4].innerHTML;
        $("#projectResultBatch").val(value1);
        $("#projectResultStudent").val(value2);
        $("#projectResultProject").val(value3);
        $("#projectResultProjectResult").val(value4);
        //编辑框确认按钮监听事件
        $("#projectResultclickUpdateGetInfoSureBtn").unbind("click").click(function () {
            //传输数据
           var result=$("#projectResultProjectResult").val();
           $.ajax({
              url:"/hospital/result/update.action",
              type:"POST",
              contentType:"application/json;charset=utf-8",
              dataType:"JSON",
              data:JSON.stringify({"id":id,"phyExmConclusion":result}),
              success:function (data) {
                  var $list = $("#tb_projectResult");
                  $("#tb_projectResult tr:not(:first)").html("");
                  $.each(data, function (index, ele) {
                      var $item = createDataItem(index, ele);
                      $list.append($item);
                  });
                  alert("修改成功");
              } ,
               error:function (data) {
                   alert("修改失败");
               }
           });
            $(".projectResultUpdate").css("display","none");
            $("#over").css("display","none");
        });
        //编辑框返回按钮监听事件
        $("#projectResultclickUpdateGetInfoBackBtn").click(function () {
            $(".projectResultUpdate").css("display","none");
            $("#over").css("display","none");
        });
    });
    //删除按钮监听事件
    $("#tb_projectResult").delegate(".projectResultInformationShowCancelBtn","click",function () {
        // alert("删除操作");
        var id=$(this).attr("data-id");//获取id
        $.ajax({
            url:"/hospital/result/delete.action",
            type:"POST",
            contentType:"application/json;charset=utf-8",
            dataType:"JSON",
            data:JSON.stringify({"id":id}),
            success:function (data) {
                var $list = $("#tb_projectResult");
                $("#tb_projectResult tr:not(:first)").html("");
                $.each(data, function (index, ele) {
                    var $item = createDataItem(index, ele);
                    $list.append($item);
                });
                alert("删除成功");
            } ,
            error:function (data) {
                alert("删除失败");
            }
        });
    });

    //测试测试
    var batchs = ["第一批次","第二批次"];
    var student1 = ["zs","ls","ww"];
    var student2 = ["aa","bb","cc"];
    var pro1 = ["p1","p2","p3","p4"];
    var pro2 = ["r1","r2"];
    for (var i=0;i<batchs.length;i++){
        $("#s1").append("<option value=\"\">"+batchs[i]+"</option>");
    }
    $("#s1").change(function () {
        $("#s2").html("");
        $(".projectResultAddNewResultInput").html("");
        var batch = $(this).children("option:selected").text();
        // alert(batch);
        if(batch === "第一批次"){
            for (var j =0; j < student1.length; j++){
                $("#s2").append("<option value=\"\">"+student1[j]+"</option>");
            }
            for(var k =0 ;k<pro1.length;k++){
                $(".projectResultAddNewResultInput").append(" <div>\n" +
                    "    <label>"+pro1[k]+"</label>\n" +
                    "    <input type=\"text\" placeholder=\"请输入体检结果\">\n" +
                    "        </div>")
            }
        }else if(batch === "第二批次"){
            for (var j =0; j < student2.length; j++){
                $("#s2").append("<option value=\"\">"+student2[j]+"</option>");
            }
            for(var k =0 ;k<pro2.length;k++){
                $(".projectResultAddNewResultInput").append(" <div>\n" +
                    "    <label>"+pro2[k]+"</label>\n" +
                    "    <input type=\"text\" placeholder=\"请输入体检结果\">\n" +
                    "        </div>")
            }
        }
    });//测试结束
    $("#projectResultAddNewResultSureBtn").click(function () {
        alert("添加成功！");
    })
    $("#projectResultAddNewResultCancelBtn").click(function () {
        alert("取消操作");
    })
});